package com.heima.controller;

import com.heima.common.AliOssUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import static java.util.UUID.randomUUID;


// 公共接口
@RestController
@RequestMapping("/api/vm-service/sku")
@Slf4j
@RequiredArgsConstructor
public class CommonController {
    //文件上传
    private final AliOssUtil aliOssUtil;

    @PostMapping("/fileUpload")
    private  String fileUpload(MultipartFile fileName) {
        try {
            String filename = fileName.getOriginalFilename();
            String substring = filename.substring(filename.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString()+ substring;
            String upload = aliOssUtil.upload(fileName.getBytes(), newFileName);
            log.info("文件上传成功____________{}",newFileName);
            return upload;

        } catch (IOException e) {
           log.error("文件上传失败",e);
        }
        return null;
    }
}
